<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" href="/favicon.svg" sizes="any">
    <title>{{.Title}} - Obsidian Share</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Helvetica, Arial, sans-serif;
            line-height: 1.6;
            color: #e6edf3;
            background-color: #0d1117;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 40px 20px;
        }

        .header {
            padding: 40px 0 20px 0;
            margin-bottom: 30px;
            border-bottom: 1px solid #30363d;
        }

        .title-row {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            gap: 20px;
            margin-bottom: 10px;
        }

        .document-title {
            font-size: 2.5em;
            font-weight: 700;
            color: #e6edf3;
            flex: 1;
        }

        .back-to-list-btn {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 8px 16px;
            background-color: #21262d;
            color: #e6edf3;
            text-decoration: none;
            border: 1px solid #30363d;
            border-radius: 6px;
            font-size: 0.9em;
            font-weight: 500;
            transition: all 0.2s;
            white-space: nowrap;
            flex-shrink: 0;
        }

        .back-to-list-btn:hover {
            background-color: #30363d;
            border-color: #58a6ff;
            color: #58a6ff;
        }

        .back-to-list-btn svg {
            flex-shrink: 0;
        }

        /* 操作按钮容器 */
        .action-buttons {
            display: flex;
            gap: 10px;
            flex-shrink: 0;
        }

        /* 下载按钮样式 */
        .download-btn {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 8px 16px;
            background-color: #238636;
            color: #ffffff;
            text-decoration: none;
            border: 1px solid #238636;
            border-radius: 6px;
            font-size: 0.9em;
            font-weight: 500;
            transition: all 0.2s;
            white-space: nowrap;
            flex-shrink: 0;
        }

        .download-btn:hover {
            background-color: #2ea043;
            border-color: #2ea043;
            color: #ffffff;
        }

        .download-btn svg {
            flex-shrink: 0;
        }

        .document-meta {
            font-size: 0.9em;
            color: #8b949e;
        }

        /* 标签样式 */
        .document-tags {
            margin: 12px 0;
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
        }

        .document-tags .tag {
            display: inline-block;
            padding: 4px 12px;
            font-size: 0.85em;
            font-weight: 500;
            color: #58a6ff;
            background-color: rgba(56, 139, 253, 0.1);
            border-radius: 12px;
            transition: all 0.2s ease;
        }

        .document-tags .tag:hover {
            background-color: rgba(56, 139, 253, 0.2);
        }

        .document-content {
            color: #e6edf3;
        }

        .document-content h1,
        .document-content h2,
        .document-content h3,
        .document-content h4,
        .document-content h5,
        .document-content h6 {
            margin-top: 24px;
            margin-bottom: 16px;
            font-weight: 600;
            line-height: 1.25;
            color: #e6edf3;
        }

        .document-content h1 {
            font-size: 2em;
            border-bottom: 1px solid #30363d;
            padding-bottom: 0.3em;
        }

        .document-content h2 {
            font-size: 1.5em;
            border-bottom: 1px solid #30363d;
            padding-bottom: 0.3em;
        }

        .document-content h3 { font-size: 1.25em; }
        .document-content h4 { font-size: 1em; }
        .document-content h5 { font-size: 0.875em; }
        .document-content h6 { font-size: 0.85em; color: #8b949e; }

        .document-content p {
            margin-bottom: 16px;
        }

        .document-content a {
            color: #58a6ff;
            text-decoration: none;
        }

        .document-content a:hover {
            text-decoration: underline;
        }

        .document-content ul,
        .document-content ol {
            padding-left: 2em;
            margin-bottom: 16px;
        }

        .document-content li {
            margin-bottom: 4px;
        }

        .document-content code {
            padding: 0.2em 0.4em;
            margin: 0;
            font-size: 85%;
            background-color: rgba(110, 118, 129, 0.4);
            border-radius: 6px;
            font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace;
            color: #e6edf3;
        }

        .document-content pre {
            padding: 16px;
            overflow: auto;
            font-size: 85%;
            line-height: 1.45;
            background-color: #161b22;
            border-radius: 6px;
            margin-bottom: 16px;
            border: 1px solid #30363d;
        }

        .document-content pre code {
            padding: 0;
            margin: 0;
            background: transparent;
            border: 0;
            color: #e6edf3;
        }

        .document-content blockquote {
            padding: 0 1em;
            margin-bottom: 16px;
            color: #8b949e;
            border-left: 0.25em solid #30363d;
        }

        .document-content table {
            border-spacing: 0;
            border-collapse: collapse;
            margin-bottom: 16px;
            width: 100%;
        }

        .document-content table th,
        .document-content table td {
            padding: 6px 13px;
            border: 1px solid #30363d;
        }

        .document-content table th {
            font-weight: 600;
            background-color: #161b22;
        }

        .document-content table tr:nth-child(even) {
            background-color: #161b22;
        }

        .document-content img {
            max-width: 100%;
            height: auto;
            border-radius: 6px;
            margin: 16px 0;
        }

        .document-content hr {
            height: 0.25em;
            padding: 0;
            margin: 24px 0;
            background-color: #30363d;
            border: 0;
        }

        /* Mermaid diagram styles */
        .mermaid-wrapper {
            margin: 20px 0;
            padding: 20px;
            background-color: #161b22;
            border-radius: 6px;
            overflow-x: auto;
            border: 1px solid #30363d;
        }

        .mermaid-wrapper .mermaid {
            display: flex;
            justify-content: center;
            background: transparent;
            padding: 0;
            margin: 0;
        }

        /* 文档布局 */
        .document-layout {
            display: flex;
            min-height: 100vh;
        }

        /* 目录侧边栏 */
        .toc-sidebar {
            position: fixed;
            left: 0;
            top: 0;
            width: 280px;
            height: 100vh;
            background-color: #161b22;
            border-right: 1px solid #30363d;
            overflow-y: auto;
            padding: 20px;
            z-index: 9998;
            transition: transform 0.3s ease;
            display: flex;
            flex-direction: column;
        }

        .toc-sidebar.collapsed {
            transform: translateX(-100%);
        }

        .toc-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #30363d;
        }

        .toc-header h3 {
            font-size: 1.2em;
            font-weight: 600;
            margin: 0;
            color: #e6edf3;
        }

        .toc-toggle {
            display: none;
            background: none;
            border: none;
            cursor: pointer;
            padding: 4px;
            color: #8b949e;
            border-radius: 6px;
            transition: background-color 0.2s;
        }

        .toc-toggle:hover {
            background-color: #30363d;
        }

        .toc-nav {
            font-size: 0.9em;
            flex: 1;
        }

        .toc-list {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .toc-item {
            margin: 4px 0;
        }

        .toc-level-1 { padding-left: 0; }
        .toc-level-2 { padding-left: 12px; }
        .toc-level-3 { padding-left: 24px; }
        .toc-level-4 { padding-left: 36px; }
        .toc-level-5 { padding-left: 48px; }
        .toc-level-6 { padding-left: 60px; }

        .toc-link {
            display: block;
            padding: 4px 8px;
            color: #8b949e;
            text-decoration: none;
            border-radius: 6px;
            transition: all 0.2s;
            line-height: 1.4;
        }

        .toc-link:hover {
            background-color: #30363d;
            color: #e6edf3;
        }

        .toc-link.active {
            background-color: #58a6ff;
            color: #ffffff;
            font-weight: 500;
        }

        /* 目录底部链接 */
        .toc-footer {
            margin-top: auto;
            padding-top: 20px;
            border-top: 1px solid #30363d;
        }

        .opensource-link {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px 12px;
            color: #8b949e;
            text-decoration: none;
            border-radius: 6px;
            transition: all 0.2s;
            font-size: 0.9em;
        }

        .opensource-link:hover {
            background-color: #30363d;
            color: #e6edf3;
        }

        .gitee-logo {
            flex-shrink: 0;
            color: #c71d23;
        }

        .external-link-icon {
            flex-shrink: 0;
            opacity: 0.6;
            transition: opacity 0.2s;
        }

        .opensource-link:hover .external-link-icon {
            opacity: 1;
        }

        /* 二维码容器样式 */
        .qrcode-container {
            padding: 16px 12px;
            margin-bottom: 16px;
            background-color: #0d1117;
            border: 1px solid #30363d;
            border-radius: 6px;
            text-align: center;
        }

        .qrcode-header {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
            margin-bottom: 12px;
            font-size: 0.9em;
            font-weight: 500;
            color: #e6edf3;
        }

        .qrcode-canvas {
            display: flex;
            justify-content: center;
            align-items: center;
            margin: 0 auto;
            padding: 8px;
            background-color: #ffffff;
            border-radius: 4px;
        }

        .qrcode-canvas img,
        .qrcode-canvas canvas {
            max-width: 100%;
            height: auto;
            display: block;
            margin: 0 auto;
        }

        .qrcode-hint {
            margin-top: 8px;
            margin-bottom: 0;
            font-size: 0.75em;
            color: #8b949e;
        }

        /* 主内容区域 */
        .main-content {
            flex: 1;
            margin-left: 280px;
            transition: margin-left 0.3s ease;
        }

        .toc-sidebar.collapsed ~ .main-content {
            margin-left: 0;
        }

        /* 固定的移动端目录切换按钮 */
        .toc-toggle-mobile {
            display: none;
            position: fixed;
            top: 20px;
            left: 20px;
            z-index: 9999;
            background-color: #161b22;
            border: 1px solid #30363d;
            border-radius: 6px;
            padding: 8px;
            cursor: pointer;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
            transition: all 0.2s;
        }

        .toc-toggle-mobile:hover {
            background-color: #30363d;
            border-color: #58a6ff;
        }

        .toc-toggle-mobile svg {
            display: block;
            color: #e6edf3;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                padding: 60px 15px 20px 15px; /* 增加顶部内边距为按钮留出空间 */
                max-width: 100%;
            }

            .document-title {
                font-size: 1.8em;
            }

            .title-row {
                flex-direction: column;
                gap: 10px;
                margin-top: 10px; /* 为标题行添加顶部外边距 */
            }

            .back-to-list-btn {
                align-self: flex-start;
            }

            /* 移动端隐藏下载按钮 */
            .download-btn {
                display: none;
            }

            /* 手机端隐藏二维码（用户已经在手机上了） */
            .qrcode-container {
                display: none !important;
            }

            /* 显示固定的切换按钮 */
            .toc-toggle-mobile {
                display: block !important;
            }

            /* 隐藏侧边栏内的切换按钮 */
            .toc-toggle {
                display: none;
            }

            .toc-sidebar {
                box-shadow: 2px 0 8px rgba(0, 0, 0, 0.3);
            }

            .main-content {
                margin-left: 0;
            }

            /* 文档内容字体和间距调整 */
            .document-content {
                font-size: 16px;
            }

            .document-content h1 {
                font-size: 1.75em;
            }

            .document-content h2 {
                font-size: 1.5em;
            }

            .document-content h3 {
                font-size: 1.25em;
            }

            .document-content pre {
                font-size: 14px;
                padding: 12px;
                overflow-x: auto;
            }

            .document-content table {
                font-size: 14px;
                display: block;
                overflow-x: auto;
                white-space: nowrap;
            }

            .document-meta {
                font-size: 0.85em;
            }

            .document-tags .tag {
                font-size: 0.8em;
                padding: 3px 10px;
            }
        }

        @media (max-width: 480px) {
            .container {
                padding: 15px 10px;
            }

            .document-title {
                font-size: 1.5em;
            }

            .document-content h1 {
                font-size: 1.5em;
            }

            .document-content h2 {
                font-size: 1.3em;
            }

            .back-to-list-btn {
                font-size: 0.85em;
                padding: 6px 12px;
            }

            /* 目录侧边栏宽度调整 */
            .toc-sidebar {
                width: 240px;
            }
        }

        @media print {
            .toc-sidebar {
                display: none;
            }

            .main-content {
                margin-left: 0;
            }
        }
    </style>
    <script type="module">
        import mermaid from 'https://registry.npmmirror.com/mermaid/latest/files/dist/mermaid.esm.min.mjs';
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            securityLevel: 'loose',
        });
    </script>
</head>
<body>
    <!-- 固定的目录切换按钮（移动端显示） -->
    <button class="toc-toggle-mobile" id="tocToggleMobile" aria-label="切换目录">
        <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <line x1="3" y1="12" x2="21" y2="12"></line>
            <line x1="3" y1="6" x2="21" y2="6"></line>
            <line x1="3" y1="18" x2="21" y2="18"></line>
        </svg>
    </button>

    <div class="document-layout">
        <!-- 目录侧边栏 -->
        <aside class="toc-sidebar collapsed" id="tocSidebar">
            <div class="toc-header">
                <h3>目录</h3>
                <button class="toc-toggle" id="tocToggle" aria-label="切换目录">
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <line x1="3" y1="12" x2="21" y2="12"></line>
                        <line x1="3" y1="6" x2="21" y2="6"></line>
                        <line x1="3" y1="18" x2="21" y2="18"></line>
                    </svg>
                </button>
            </div>
            <nav class="toc-nav" id="tocNav">
                <!-- 目录将通过 JavaScript 动态生成 -->
            </nav>
            <div class="toc-footer">
                <!-- 二维码显示区域 -->
                <div class="qrcode-container">
                    <div class="qrcode-header">
                        <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                            <rect x="3" y="3" width="7" height="7"></rect>
                            <rect x="14" y="3" width="7" height="7"></rect>
                            <rect x="14" y="14" width="7" height="7"></rect>
                            <rect x="3" y="14" width="7" height="7"></rect>
                        </svg>
                        <span>扫码查看</span>
                    </div>
                    <div id="qrcode" class="qrcode-canvas"></div>
                    <p class="qrcode-hint">使用手机扫码访问</p>
                </div>

                <a href="https://gitee.com/miya0716/ob-share" target="_blank" rel="noopener noreferrer" class="opensource-link">
                    <svg class="gitee-logo" viewBox="0 0 24 24" width="20" height="20">
                        <path fill="currentColor" d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.037a.594.594 0 0 1-.592-.593v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/>
                    </svg>
                    <span>开源项目</span>
                    <svg class="external-link-icon" viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path>
                        <polyline points="15 3 21 3 21 9"></polyline>
                        <line x1="10" y1="14" x2="21" y2="3"></line>
                    </svg>
                </a>
            </div>
        </aside>

        <!-- 主内容区域 -->
        <div class="main-content">
            <div class="container">
                <div class="header">
                    <div class="title-row">
                        <h1 class="document-title">{{.Title}}</h1>
                        <div class="action-buttons">
                            <a href="/d/{{.UUID}}/download" class="download-btn" title="下载原始 Markdown 文档" target="_blank">
                                <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                    <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
                                    <polyline points="7 10 12 15 17 10"></polyline>
                                    <line x1="12" y1="15" x2="12" y2="3"></line>
                                </svg>
                                <span>下载</span>
                            </a>
                            <a href="/documents?user={{.UserUID}}" class="back-to-list-btn" title="返回列表">
                                <svg viewBox="0 0 16 16" width="20" height="20" fill="currentColor">
                                    <path d="M1.75 1h12.5c.966 0 1.75.784 1.75 1.75v10.5A1.75 1.75 0 0 1 14.25 15H1.75A1.75 1.75 0 0 1 0 13.25V2.75C0 1.784.784 1 1.75 1ZM1.5 2.75v10.5c0 .138.112.25.25.25h12.5a.25.25 0 0 0 .25-.25V2.75a.25.25 0 0 0-.25-.25H1.75a.25.25 0 0 0-.25.25Zm7.03 4.28a.75.75 0 0 1 0 1.06l-2.5 2.5a.75.75 0 0 1-1.06 0l-1-1a.75.75 0 0 1 1.06-1.06l.47.47 1.97-1.97a.75.75 0 0 1 1.06 0Z"></path>
                                </svg>
                                <span>文档列表</span>
                            </a>
                        </div>
                    </div>

                    {{if .Tags}}
                    <div class="document-tags">
                        {{range .Tags}}
                        <span class="tag">#{{.}}</span>
                        {{end}}
                    </div>
                    {{end}}

                    <div class="document-meta">
                        <span>作者: {{.Author}}</span>
                        <span style="margin: 0 8px;">•</span>
                        <span>浏览次数: {{.Views}}</span>
                        <span style="margin: 0 8px;">•</span>
                        <span>创建于: {{.CreatedAt}}</span>
                    </div>
                </div>

                <div class="document-content" id="documentContent">
                    {{.ContentHTML}}
                </div>
            </div>
        </div>
    </div>

    <script src="https://registry.npmmirror.com/qrcodejs/1.0.0/files/qrcode.min.js"></script>
    <script>
    // 生成目录
    document.addEventListener('DOMContentLoaded', function() {
        // 生成二维码
        const currentUrl = window.location.href;
        const qrcodeContainer = document.getElementById('qrcode');

        if (qrcodeContainer && typeof QRCode !== 'undefined') {
            new QRCode(qrcodeContainer, {
                text: currentUrl,
                width: 200,
                height: 200,
                colorDark: '#000000',
                colorLight: '#ffffff',
                correctLevel: QRCode.CorrectLevel.M
            });
        }

        const content = document.getElementById('documentContent');
        const tocNav = document.getElementById('tocNav');
        const tocSidebar = document.getElementById('tocSidebar');
        const tocToggle = document.getElementById('tocToggle');

        // 立即设置移动端目录状态
        if (window.innerWidth <= 768) {
            tocSidebar.classList.add('collapsed');
        } else {
            tocSidebar.classList.remove('collapsed');
        }

        // 提取所有标题
        const headings = content.querySelectorAll('h1, h2, h3, h4, h5, h6');

        if (headings.length === 0) {
            // 如果没有标题，隐藏目录
            tocSidebar.style.display = 'none';
            document.querySelector('.main-content').style.marginLeft = '0';
            return;
        }

        // 为每个标题添加 ID
        headings.forEach((heading, index) => {
            if (!heading.id) {
                heading.id = 'heading-' + index;
            }
        });

        // 生成目录 HTML
        let tocHTML = '<ul class="toc-list">';
        headings.forEach((heading) => {
            const level = parseInt(heading.tagName.substring(1));
            const text = heading.textContent;
            const id = heading.id;

            tocHTML += `<li class="toc-item toc-level-${level}">
                <a href="#${id}" class="toc-link" data-target="${id}">${text}</a>
            </li>`;
        });
        tocHTML += '</ul>';

        tocNav.innerHTML = tocHTML;

        // 目录点击事件
        const tocLinks = tocNav.querySelectorAll('.toc-link');
        tocLinks.forEach(link => {
            link.addEventListener('click', function(e) {
                e.preventDefault();
                const targetId = this.getAttribute('data-target');
                const target = document.getElementById(targetId);

                if (target) {
                    // 平滑滚动到目标
                    target.scrollIntoView({ behavior: 'smooth', block: 'start' });

                    // 更新 URL hash（不触发滚动）
                    history.pushState(null, null, '#' + targetId);

                    // 高亮当前项
                    tocLinks.forEach(l => l.classList.remove('active'));
                    this.classList.add('active');
                }
            });
        });

        // 滚动时高亮当前章节
        let ticking = false;
        function updateActiveHeading() {
            const scrollPosition = window.scrollY + 100;

            let currentHeading = null;
            headings.forEach(heading => {
                if (heading.offsetTop <= scrollPosition) {
                    currentHeading = heading;
                }
            });

            if (currentHeading) {
                tocLinks.forEach(link => {
                    link.classList.remove('active');
                    if (link.getAttribute('data-target') === currentHeading.id) {
                        link.classList.add('active');
                    }
                });
            }

            ticking = false;
        }

        window.addEventListener('scroll', function() {
            if (!ticking) {
                window.requestAnimationFrame(updateActiveHeading);
                ticking = true;
            }
        });

        // 初始化高亮
        updateActiveHeading();

        // 移动端目录切换（侧边栏内的按钮）
        tocToggle.addEventListener('click', function() {
            tocSidebar.classList.toggle('collapsed');
        });

        // 移动端目录切换（固定的外部按钮）
        const tocToggleMobile = document.getElementById('tocToggleMobile');
        if (tocToggleMobile) {
            tocToggleMobile.addEventListener('click', function() {
                tocSidebar.classList.toggle('collapsed');
            });
        }

        // 响应式处理
        function handleResize() {
            if (window.innerWidth <= 768) {
                // 移动端：强制隐藏目录
                tocSidebar.classList.add('collapsed');
            } else {
                // 桌面端：强制显示目录
                tocSidebar.classList.remove('collapsed');
            }
        }

        window.addEventListener('resize', handleResize);
        handleResize();
    });
    </script>
</body>
</html>
